package edu.gduf.config;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by mazhenhua on 2016/12/27.
 *
 * 过滤器
 */
public class SessionFilter implements Filter {



    /**
     * 封装，不需要过滤的list列表
     */
  

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;

		String requestURI = request.getRequestURI();
		if (pattern(requestURI)) {

			chain.doFilter(servletRequest, servletResponse);
			return;
		}
		HttpSession session = request.getSession();
		Object attribute = session.getAttribute("teacher");
		if (attribute != null) {
			chain.doFilter(servletRequest, servletResponse);
			return;
		}
		request.getRequestDispatcher("/index.jsp").forward(servletRequest, servletResponse);


    }

    @Override
    public void destroy() {

    }

	private boolean pattern(String uri) {
		System.out.println("uri:" + uri);
		if (uri.endsWith(".js") || uri.endsWith(".css") || uri.endsWith(".jpg") || uri.endsWith(".png")
				|| uri.endsWith(".html") || uri.endsWith(".gif") || uri.startsWith("/easyui/")
				|| uri.startsWith("/css/") || uri.startsWith("/img/")) {
			return true;
		} else if (uri.endsWith("index.jsp") || uri.endsWith("login.jsp") || uri.contains("/signin/verify")
				|| uri.contains("/signin/teacher/login") || uri.contains("/signin/insert")
				|| uri.contains("/signin/signinjsp")
 || uri.contains("signin.jsp") || uri.contains("/favicon.ico")) {
			return true;
		}
		return false;

	}


  
}